﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Site_SetUp_NoSite : BaseSitePage
{
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        dsSite.SelectData += new EventHandler<Acms.DataControls.SelectEventArgs<Acms.Bll.Site>>(dsSite_SelectData);
        grid.ItemCommand += new Telerik.Web.UI.GridCommandEventHandler(grid_ItemCommand);
        btnCreateNew.Click += new EventHandler(btnCreateNew_Click);
    }

    void btnCreateNew_Click(object sender, EventArgs e)
    {
        if (IsValid)
        {
            Acms.Bll.Site site = Acms.Bll.Site.CreateInstance();
            site.Name = txtSiteName.Text.Trim();
            site.Root = GetSiteRoot();
            site.Update();
            Redirect(site);
        }
    }

    void grid_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
    {
        if (e.CommandName == "SelectSite")
        {
            Acms.Bll.Site site = Sites[e.Item.ItemIndex];
            Acms.Bll.SiteMapping mapping = Acms.Bll.SiteMapping.CreateInstance();
            mapping.SiteId = site.Id;
            mapping.MappingName = GetSiteRoot();
            mapping.Update();
            Redirect(site);
        }
    }

    private void Redirect(Acms.Bll.Site site)
    {
        //check that there is a developer set up
        List<Acms.Bll.User> users = Acms.Bll.User.GetAllByRoleAndSiteId(Acms.Bll.Role.Roles.Developer, site.Id);
        if (users == null && Request.IsAuthenticated)
        {
            Acms.Bll.UserRole role = Acms.Bll.UserRole.CreateInstance(User.Identity.User, Acms.Bll.Role.Roles.Developer, AcmsContext.Site);
            role.Update();
            Response.Redirect("~/", true);
        }
        else if(users == null)
        {
            Response.Redirect("~/Site/SetUp/NoAdmin.aspx", true);
        }
        else
        {
            Response.Redirect("~/", true);
        }
    }

    void dsSite_SelectData(object sender, Acms.DataControls.SelectEventArgs<Acms.Bll.Site> e)
    {
        e.SetData(Sites);
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DataBind();
        }

    }

    public override Acms.Bll.Role.Roles[] AllowedRoles
    {
        get { return new Acms.Bll.Role.Roles[] { Acms.Bll.Role.Roles.Registered, Acms.Bll.Role.Roles.Anonomous }; }
    }

    public string GetSiteRoot()
    {
        System.Text.StringBuilder tmp = new System.Text.StringBuilder();
        tmp.Append(Request.ServerVariables["Server_Name"]);
        tmp.Append(Request.ApplicationPath.Substring(0, Request.ApplicationPath.ToLower().IndexOf("/acms")));
        return tmp.ToString();
    }

    private List<Acms.Bll.Site> sites;
    protected List<Acms.Bll.Site> Sites
    {
        get
        {
            if (sites == null)
            {
                sites = Acms.Bll.Site.GetAll();
            }
            return sites;
        }
    }

}
